package fun.ui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.SplashScreen;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;

/**
 * SplashScreen<br>
 * ����Ϊ:<br>
 * 
 * <pre>
 * +----------------+
 * |				|
 * .				.
 * .	  LOGO    	.
 * .				.
 * |				|
 * |----------------|
 * |  Status Label  |
 * +----------------+
 * </pre>
 * 
 * <br>
 * ������ǻ�һ������ :p
 */
public class Splash {
	public static final int MAX_PROGRESS = 3;
	static int progress = 0;
	// ========================================
	private static SplashScreen splash = null;
	private static Graphics2D g2 = null;
	private static Path2D path = null;
	private static int width = 0;
	private static double height2 = 0;
	private static double maxX = Math.PI * 2;
	static {
		splash = SplashScreen.getSplashScreen();
		if (null != splash) {
			Dimension size = splash.getSize();
			g2 = splash.createGraphics();
			g2.setStroke(new BasicStroke(2));
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			path = new Path2D.Double();
			width = size.width;
			height2 = size.height / 2.0D - 6;
			path.moveTo(0, size.height / 2.0D);
			// ===========================
			StatusLabel.initialize(size);
		}
	}

	public static void setStatus(String status) {
		if (isOpen()) {
			StatusLabel.setStatus(g2, status);
			advance();
			splash.update();
		}
	}

	private static void advance() {
		if (isOpen()) {
			double x = 0;
			double y = 0;
			int oldPos = progress * width / MAX_PROGRESS;
			progress++;
			int newPos = progress * width / MAX_PROGRESS;
			for (int i = oldPos; i < newPos; i++) {
				x = i * maxX / width;
				y = Math.sin(x) * height2;
				y = height2 - y + 6;
				path.lineTo(i, y);
			}
			g2.setColor(Color.blue);
			g2.draw(path);
		}
	}

	private static boolean isOpen() {
		return null != SplashScreen.getSplashScreen();
	}

	// StatusLabel
	static class StatusLabel {
		// text insets
		static final int top = 0;
		static final int left = 6;
		static final int bottom = 0;
		static final int right = 5;
		// text size & position
		static int textHeight = 0;
		static int textPos = 0;
		static Rectangle2D.Float bounds = null;
		// color
		static Color back = Color.black;
		static Color fore = Color.white;

		static void initialize(Dimension splashSize) {
			FontMetrics fm = g2.getFontMetrics();
			textHeight = fm.getHeight() + top + bottom;
			textPos = (textHeight + fm.getAscent() - fm.getLeading() - fm.getDescent()) / 2;
			int width = splashSize.width;
			StatusLabel.bounds = new Rectangle2D.Float(0, splashSize.height - textHeight, width, textHeight);
		}

		static void setStatus(Graphics2D g2, String status) {
			g2.setColor(Color.white);
			g2.fill(bounds);
			g2.setColor(Color.black);
			g2.drawString(status, StatusLabel.bounds.x + left, StatusLabel.bounds.y + textPos);
		}
	}
}
